package com.samapp.indent;

import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.samapp.R;
import com.samapp.entity.Indent;

public class IndentAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private List<Indent> mIndents;
	private Context context;

	public IndentAdapter(Context context, List<Indent> mIndents) {
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
		this.mIndents = mIndents;
	}

	@Override
	public int getCount() {
		return mIndents.size();
	}

	@Override
	public Object getItem(int position) {
		return mIndents.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.indent_item, parent, false);
			holder = new ViewHolder();
			holder.orderID = (TextView) convertView.findViewById(R.id.textview_orderID);
			holder.mClient = (TextView) convertView.findViewById(R.id.textview_client);
			holder.detectionItem = (TextView) convertView.findViewById(R.id.textview_detectionItem);
			holder.detectionMethod = (TextView) convertView.findViewById(R.id.textview_detectionMethod);
			holder.btnDeleteIndent = (Button) convertView.findViewById(R.id.btn_delete_indent);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		Indent mIndent = mIndents.get(position);
		holder.orderID.setText(mIndent.getNumber());
		holder.mClient.setText(mIndent.getCnumber());
		holder.detectionItem.setText("" + mIndent.getOrderDetail().size());
		holder.detectionMethod.setText("" + mIndent.getOrderDetail().size());
		holder.btnDeleteIndent.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Dialog alertDialog = new AlertDialog.Builder(context).setTitle("删除订单").setMessage("您确定删除该订单吗？").setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						mIndents.remove(position);
						((IndentManageActivity) context).onRefresh();
					}
				}).setNegativeButton("取消", null).create();
				alertDialog.show();
			}
		});
		return convertView;
	}

	private class ViewHolder {
		TextView orderID;
		TextView mClient;
		TextView detectionItem;
		TextView detectionMethod;
		Button btnDeleteIndent;
	}
}
